package com.lovepick.http;

import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Log;

import com.lovepick.GlobalApp;
import com.lovepick.util.Constant;

public class HttpHelper
{
	private static final String HTTP_TAG = "HttpHelper";
	private static final int TIMEOUT = 10000;
	private DefaultHttpClient mHttpClient = null;
	private HttpGet mHttpGet = null;
	private HttpPost mHttpPost = null;
	private HttpParams mHttpParams = null;
	
	public HttpHelper()
	{
		mHttpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(mHttpParams, TIMEOUT);
	}

	public String doGet(String url)
	{
		String strResult = null;
		try
		{
			mHttpClient = new DefaultHttpClient(mHttpParams);
			//mHttpClient = new DefaultHttpClient();
			Log.i(HTTP_TAG, "###url###");
			Log.i(HTTP_TAG, url);

			mHttpGet = new HttpGet(url);
			Log.i(HTTP_TAG, "-----doGet-----");
			if (GlobalApp.getInstance().getCookie() != null)
			{
				Log.i(HTTP_TAG, "header cookie: " + GlobalApp.getInstance().getCookie());
				mHttpGet.setHeader("Cookie", GlobalApp.getInstance().getCookie());
			}

			HttpResponse httpResponse = mHttpClient.execute(mHttpGet);

			int statusCode = httpResponse.getStatusLine().getStatusCode();
			Log.i(HTTP_TAG, "###statusLine: " + httpResponse.getStatusLine().toString());
			if (statusCode == HttpStatus.SC_OK)
			{
				HttpEntity httpEntity = httpResponse.getEntity();
				if (httpEntity != null)
				{
					strResult = EntityUtils.toString(httpEntity);
					Log.i(HTTP_TAG, "---sc_ok http_result---");
					Log.i(HTTP_TAG, strResult);

					List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
					if (!cookies.isEmpty())
					{
						Log.i(HTTP_TAG, "---saving cookies---");
						GlobalApp.getInstance().setCookie(cookies);
						Log.i(HTTP_TAG, cookies.toString());
					}
				}
				else
				{
					Log.i(HTTP_TAG, "doGet: httpEntity null");
					strResult = Constant.HTTP_ERROR;
				}

			}
			else
			{
				strResult = Constant.HTTP_ERROR;
			}
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block			
			Log.i(HTTP_TAG, "Get failure: " + e.getMessage());
			Log.i(HTTP_TAG, "strResult: " + strResult);
			strResult = Constant.HTTP_ERROR;
			e.printStackTrace();			
		}
		finally
		{
			this.abort();
		}
		return strResult;
	}

	public String doPost(String url, List<NameValuePair> parameters)
	{
		String strResult = null;
		try
		{
			Log.i(HTTP_TAG, "-----doPost-----");
			mHttpClient = new DefaultHttpClient(mHttpParams);
			//mHttpClient = new DefaultHttpClient();
			Log.i(HTTP_TAG, "###url###\n" + url);
			// 建立HttpPost对象
			mHttpPost = new HttpPost(url);

			if (parameters != null)
			{
				Log.i(HTTP_TAG, "parameters: " + parameters.toString());

				// 添加请求参数到请求对象
				mHttpPost.setEntity(new UrlEncodedFormEntity(parameters, HTTP.UTF_8));
			}
			if (GlobalApp.getInstance().getCookie() != null)
			{
				Log.i(HTTP_TAG, "###add cookies to the header###");
				mHttpPost.setHeader("Cookie", GlobalApp.getInstance().getCookie());
			}

			// 发送请求并等待响应
			HttpResponse httpResponse = mHttpClient.execute(mHttpPost);

			// 获取状态码
			int statusCode = httpResponse.getStatusLine().getStatusCode();
			Log.i(HTTP_TAG, "statusLine: " + httpResponse.getStatusLine().toString());

			if (statusCode == HttpStatus.SC_OK)
			{
				// 读取返回数据
				HttpEntity entity = httpResponse.getEntity();
				if (entity != null)
				{
					strResult = EntityUtils.toString(entity);
					Log.i(HTTP_TAG, "-----sc_ok http_result------\n" + strResult);

					List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
					if (!cookies.isEmpty())
					{
						Log.i(HTTP_TAG, "---saving cookies---");
						GlobalApp.getInstance().setCookie(cookies);
						Log.i(HTTP_TAG, cookies.toString());
					}
				}
				else
				{
					Log.i(HTTP_TAG, "doPost: httpEntity null.");
					strResult = Constant.HTTP_ERROR;
				}
			}
			else
			{
				Log.i(HTTP_TAG, "Error Response: "
						+ httpResponse.getStatusLine().toString());
				strResult = Constant.HTTP_ERROR;
			}
		}
		catch (Exception e)
		{
			// TODO: handle exception
			Log.i(HTTP_TAG, "Post failure: " + e.getMessage());
			e.printStackTrace();
			strResult = Constant.HTTP_ERROR;
		}
		finally
		{
			this.abort();
		}
		return strResult;
	}

	public void abort()
	{
		if (mHttpGet != null)
		{
			mHttpGet.abort();
			mHttpGet = null;
		}

		if (mHttpPost != null)
		{
			mHttpPost.abort();
			mHttpPost = null;
		}

		if (mHttpClient != null)
		{
			mHttpClient.getConnectionManager().shutdown();
			mHttpClient = null;
		}
	}
}
